home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / Boxes / ScoreBoard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-05  |  4.6 KB  |  253 lines  |  [TEXT/KAHL]

  1. /*
  2.  * ScoreBoard.c - "Boxes" scoreboard manager
  3.  *    
  4.  * Port is assumed to be set correctly for routines that draw anything.
  5.  */
  6.  
  7.  
  8. # include    "Boxes.h"
  9.  
  10.  
  11. # define    frameThickness    2
  12. # define    tHeight            20        /* title height */
  13. # define    pHeight            20        /* title height */
  14. # define    pWidth            50        /* player width */
  15. # define    sWidth            50        /* score width */
  16. # define    sbWidth            (pWidth + sWidth + 3 * frameThickness)
  17.  
  18. static short    nPlayers = 2;
  19. static short    player;
  20. static short    score[maxPlayer];
  21. static Rect        sbRect;                /* scoreboard rectangle */
  22. static Rect        tRect;                /* title rectangle */
  23. static Rect        pRect;                /* player rectangle */
  24. static Rect        sRect;                /* score rectangle */
  25.  
  26. static RgnHandle    arrowRgn = (RgnHandle) nil;
  27.  
  28.  
  29. /* Local routine prototypes */
  30.  
  31. static void    DrawPlayer (short i);
  32. static void    HilitePlayer (short i);
  33. static void    HiliteScore (short i);
  34.  
  35.  
  36. short
  37. GetPlayerCount (void)
  38. {
  39.     return (nPlayers);
  40. }
  41.  
  42.  
  43. void
  44. SetPlayerCount (short n)
  45. {
  46.     nPlayers = n;
  47.     if (nPlayers < 1)
  48.         nPlayers = 1;
  49.     if (nPlayers > maxPlayer)
  50.         nPlayers = maxPlayer;
  51. }
  52.  
  53.  
  54. void
  55. InitializeScoreBoard (void)
  56. {
  57. short    i;
  58.  
  59.     player = 0;
  60.     for (i = 0; i < nPlayers; i++)
  61.         score[i] = 0;
  62.     if (arrowRgn == nil)
  63.     {
  64.         /*
  65.             Create arrow region
  66.         */
  67.         arrowRgn = NewRgn ();
  68.         OpenRgn ();
  69.         MoveTo (0, 3);
  70.         LineTo (4, 3);
  71.         LineTo (4, 0);
  72.         LineTo (12, 7);
  73.         LineTo (4, 14);
  74.         LineTo (4, 11);
  75.         LineTo (0, 11);
  76.         LineTo (0, 3);
  77.         CloseRgn (arrowRgn);
  78.     }
  79. }
  80.  
  81.  
  82. /*
  83.  * Calculate size of scoreboard.  (With upper left at (0, 0), i.e., this
  84.  * is not in the position it needs to be
  85.  */
  86.  
  87. void
  88. CalcScoreBoardSize (Rect *r)
  89. {
  90. short    depth;
  91.  
  92.     depth = tHeight + pHeight * nPlayers + frameThickness * 3;
  93.     SetRect (r, 0, 0, sbWidth, depth);
  94. }
  95.  
  96.  
  97. /*
  98.  * Figure out where scoreboard should be located and calculate the positions
  99.  * of some of its pieces.
  100.  */
  101.  
  102. void
  103. PlaceScoreBoard (short h, short v)
  104. {
  105.     CalcScoreBoardSize (&sbRect);
  106.     OffsetRect (&sbRect, h, v);
  107.     SetRect (&tRect, 0, 0, sbWidth, tHeight);
  108.     OffsetRect (&tRect, sbRect.left, sbRect.top);
  109.     InsetRect (&tRect, frameThickness, frameThickness);
  110.     SetRect (&pRect, 0, 0, sbWidth, tHeight);
  111.     OffsetRect (&pRect, sbRect.left, sbRect.top + 2 * frameThickness + tHeight);
  112.     InsetRect (&pRect, frameThickness, 0);
  113.     sRect = pRect;
  114.     pRect.right = pRect.left + pWidth;
  115.     sRect.left = sRect.right - sWidth;
  116. }
  117.  
  118.  
  119. void
  120. NextPlayer (void)
  121. {
  122.     DrawPlayer (player);
  123.     player = ++player % nPlayers;
  124.     HilitePlayer (player);
  125. }
  126.  
  127.  
  128. short
  129. GetPlayer (void)
  130. {
  131.     return (player);
  132. }
  133.  
  134.  
  135. void
  136. IncrementScore (short player, short n)
  137. {
  138.     score[player] += n;
  139. }
  140.  
  141.  
  142. static void
  143. DrawPlayer (short i)
  144. {
  145. Rect    r;
  146. Str255    s;
  147.  
  148.     NumToString ((long) i + 1, s);
  149.     r = pRect;
  150.     OffsetRect (&r, 0, i * pHeight);
  151.     EraseRect (&r);
  152.     MoveTo (r.right - StringWidth (s) - 5, r.bottom - fontInfo.descent);
  153.     SetColor (GetPlayerColor (i));
  154.     DrawString (s);
  155.     RestoreColor ();
  156. }
  157.  
  158.  
  159. static void
  160. HilitePlayer (short i)
  161. {
  162.     DrawPlayer (i);
  163.     OffsetRgn (arrowRgn, pRect.left + 5, pRect.top + i * pHeight + 5);
  164.     SetColor (GetPlayerColor (i));
  165.     PaintRgn (arrowRgn);
  166.     RestoreColor ();
  167.     OffsetRgn (arrowRgn, -(pRect.left + 5), -(pRect.top + i * pHeight + 5));
  168. }
  169.  
  170.  
  171. void
  172. DrawScore (short i)
  173. {
  174. Rect    r;
  175. Str255    s;
  176.  
  177.     NumToString ((long) score[i], s);
  178.     r = sRect;
  179.     OffsetRect (&r, 0, i * pHeight);
  180.     EraseRect (&r);
  181.     MoveTo (r.right - StringWidth (s) - 5, r.bottom - fontInfo.descent);
  182.     SetColor (GetPlayerColor (i));
  183.     DrawString (s);
  184.     RestoreColor ();
  185. }
  186.  
  187.  
  188. static void
  189. HiliteScore (short i)
  190. {
  191.     DrawScore (i);
  192.     OffsetRgn (arrowRgn, sRect.left + 5, sRect.top + i * pHeight + 5);
  193.     SetColor (GetPlayerColor (i));
  194.     PaintRgn (arrowRgn);
  195.     RestoreColor ();
  196.     OffsetRgn (arrowRgn, -(sRect.left + 5), -(sRect.top + i * pHeight + 5));
  197. }
  198.  
  199.  
  200. void
  201. DrawScoreBoard (void)
  202. {
  203. short    left;
  204. short    i, max;
  205. Rect    r;
  206.  
  207.     EraseRect (&sbRect);
  208.     /* draw frame */
  209.     SetColor (GetScoreboardColor ());
  210.     PenSize (frameThickness, frameThickness);
  211.     r = sbRect;
  212.     FrameRect (&r);
  213.     r.bottom = r.top + tHeight + 2 * frameThickness;
  214.     FrameRect (&r);
  215.     r = sbRect;
  216.     r.right = r.left + pWidth + 2 * frameThickness;
  217.     FrameRect (&r);
  218.     PenNormal ();
  219.     /* draw title */
  220.     r = tRect;
  221.     r.right = r.left + pWidth;
  222.     left = r.left + (r.right - r.left - StringWidth ("\pPlayer")) / 2;
  223.     MoveTo (left, r.bottom - fontInfo.descent);
  224.     DrawString ("\pPlayer");
  225.     r = tRect;
  226.     r.left = r.right - sWidth;
  227.     left = r.left + (r.right - r.left - StringWidth ("\pScore")) / 2;
  228.     MoveTo (left, r.bottom - fontInfo.descent);
  229.     DrawString ("\pScore");
  230.     RestoreColor ();
  231.  
  232.     for (i = 0; i < nPlayers; i++)
  233.     {
  234.         DrawPlayer (i);
  235.         DrawScore (i);
  236.     }
  237.     if (SidesLeft () > 0)        /* game not over */
  238.         HilitePlayer (player);
  239.     else
  240.     {
  241.         for (max = 0, i = 0; i < nPlayers; i++)
  242.         {
  243.             if (max < score[i])
  244.                 max = score[i];
  245.         }
  246.         for (i = 0; i < nPlayers; i++)
  247.         {
  248.             if (max == score[i])
  249.                 HiliteScore (i);
  250.         }
  251.     }
  252. }
  253.